#include "fxcc/core/graph/opengl3/ssao/BlurEffect.h"

using namespace fxcc::graph::opengl3::ssao;

BlurEffect::BlurEffect()
    : fxcc::graph::opengl3::Effect3d(BlurEffect::FragCode()) {
      };

void BlurEffect::Begin()
{
    m_Shader->Use();
    m_Shader->setInt("ssaoInput", 0);
    fxcc::graph::opengl3::Effect3d::Begin();
}

void BlurEffect::SetSSAOInput(const fxcc::graph::opengl3::Texture *texture)
{
    texture->Active(0);
};

std::string BlurEffect::FragCode()
{
    return R"(
    out float FragColor;

uniform sampler2D ssaoInput;

void main() 
{
    vec2 texelSize = 1.0 / vec2(textureSize(ssaoInput, 0));
    float result = 0.0;
    for (int x = -2; x < 2; ++x) 
    {
        for (int y = -2; y < 2; ++y) 
        {
            vec2 offset = vec2(float(x), float(y)) * texelSize;
            result += texture(ssaoInput, vOut.TexCoords + offset).r;
        }
    }
    FragColor = result / (4.0 * 4.0);
}  

    )";
};
